 var serviceUrl = $api.getStorage('serviceUrl');  //服务器地址
 var member=$api.getStorage('member');
 apiready = function () {
 
 
        var $header = $api.dom('header');
        $api.fixIos7Bar($header);
        var $body = $api.dom('body');
        var $footer = $api.byId('footer');
        var header_h = $api.offset($header).h;
        var body_h = $api.offset($body).h;
        var footer_h = $api.offset($footer).h;
        var rect_h = body_h - header_h - footer_h;
        api.openFrameGroup({
            name: 'group',
            scrollEnabled: false,
            rect: {x: 0, y: header_h, w: 'auto', h: rect_h},
            index: 0,
            frames: [
                {
                    name: 'index',
                    url: 'index.html'
                },
                {
                    name: 'category',
                    url: 'category.html'
                },
//              {
//                  name: 'cart',
//                  url: 'cart.html'
//              },
                {
                    name: 'user',
                    url: 'user.html'
                }
            ]
        }, function (ret, err) {
        });
        
        //监听购物车变化  
        api.addEventListener({
		    name: 'refreshCart'
		}, function(ret){
		    getCartNum();     // 重新获取购物车和首页数量
		});
        
          getCartNum();
        

    }
    

    
    // 随意切换按钮
    function randomSwitchBtn(obj, name, index) {
    
        if(name == 'user'||name=='cart'){
	    
	       var account = $api.getStorage('account');
	       if(!account){
	       

			    //登陆
			    api.openWin({
			        name: 'login',
				    url: 'login.html',
			        bounces: false,
			        vScrollBarEnabled:false,
			        hScrollBarEnabled:false,
			        reload: true,
			     
			    });
			    return;
			}
		   
			
	    }
	    
    
        var $header = $api.dom('header');
        var $titleBar = $api.domAll($header, '.topbar');
        for (var i = 0; i < $titleBar.length; i++) {
            $api.removeCls($titleBar[i], 'activebar');
        }
        $api.addCls($api.byId(name), 'activebar');
        var $footer = $api.byId('footer');
        
        var $footerBar = $api.domAll($footer, 'li');
        
        for (var j = 0; j < $footerBar.length; j++) {
            $api.removeCls($footerBar[j], 'navtive');
        }
        
        $api.addCls(obj, 'navtive');
        
        api.setFrameGroupIndex({
            name: 'group',
            index: index
        });

    }






//获取购物车数量
function getCartNum(){
     
     
     if(member!=undefined){
        $api.get(serviceUrl+'/app/member/cart?cartId='+member.cartId,function(ret){
		  		$api.setStorage('cart', ret.carts);
		  	//	$api.html($api.byId('cnum'),ret.carts.buyNum);
		 
		},'json');
     
     }else{
         //  $api.html($api.byId('cnum'),'0');
     }
     

};





//购物车
function opencart(){

    var account = $api.getStorage('account');
     if(!account){
		    //登陆
		    api.openWin({
		        name: 'login',
			    url: 'login.html',
		        bounces: false,
		        vScrollBarEnabled:false,
		        hScrollBarEnabled:false,
		        reload: true,
		     
		    });
		    return;
	}else{
	    api.openWin({
	        name:"cart",
	        url: "cart.html",
	        bounces: false,
	        vScrollBarEnabled:false,
	        hScrollBarEnabled:false,
	        reload: true,
	       }
	    );
	}



     
};






